local ServerOHOH = _G.ServerOHOH;
local M = ServerOHOH.class.new(script, ServerOHOH.files.Server_PlayerComponent(),script.Parent.Name);
local function_list = _G.ServerOHOH.function_list
local drill_cfg = ServerOHOH.config.Drill
local ItemTool=_G.CommonOHOH.load.ItemTool()
local MarketplaceService = game:GetService("MarketplaceService")
function M.new(self,...)
	return M(self,...);
end;



function M.ctor(self, parent)
	M.super.ctor(self, parent);	
    self.player=self.parent
	self.DataBase = self.player:GetComponent(ServerOHOH.load.Server_PlayerDataComponent())

end


function M.EquipDrill(self,name)
	local equipName = nil
	if typeof(name) == "number" then
		equipName = tostring(name)
	else
		equipName = name
	end
	print("Drill,Equipname:",equipName)
	if self.DataBase:GetData("drill").had[equipName] then
		self.DataBase:GetData("drill").equip = equipName
	else
		return false
	end
	self.DataBase:Execut(function()
    end);
    self.player:SynchDataToClient("drill")
    return true
end

function M.BuyDrill(self,name,id)
	if self:IsHad(name) then
		--判断购买类型分别处理
		if self:Buy(id,name) then
			return true
		else
			return false
		end
	end
end

function M.IsHad(self,name)
	if self.DataBase:GetData("drill").had[name] == nil then
		return true
	end
	return false
end

function M.Buy(self,id,name)
	local Name = nil
	if typeof(name) == "number" then
		Name = tostring(name)
	end

	local costId = drill_cfg[id].costId
	local costCount = drill_cfg[id].price

	if ItemTool.IsRubux(costId) then
		print("robux购买")
		MarketplaceService:PromptPurchase(self.parent.main_inst,drill_cfg[id].pid)
		return false
	else
		if ItemTool.IsHadEnough(costId,costCount,self.DataBase) then
			local key,id = ItemTool.GetKeyAndID(costId)
			self.DataBase:GetData(key)[id] = self.DataBase:GetData(key)[id]-costCount
			self.DataBase:Execut(function()
			end);
			self.player:SynchDataToClient("base")
		else
			print("wins不足")
			return false
		end
	end
	
	self.DataBase:GetData("drill").had[Name] = 1
	self.DataBase:Execut(function()
	end);
	self.player:SynchDataToClient("drill")
	return true
end


function M.dtor(self)
	M.super.dtor(self);
end

M:RegistFunction(function_list.equip_drill,M.EquipDrill)

M:RegistFunction(function_list.buy_drill,M.BuyDrill)

return M